<?php

namespace App\Http\Controllers\ClubLeader;

use App\Club;
use App\ClubLeader;
use Hash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ClubController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $club_id = $request->session()->get('club_id');
        $club = Club::where('id',$club_id)->first();
        return view('ClubLeader.clubInfo', compact('club'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('ClubLeader.resetPassword');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules = [
            'password' => 'required',
            'newPassword' => 'required|min:6',
            'passwordAgain' => 'required|min:6'
        ];
        $this->validate($request, $rules);
        $password = $request->get('password');
        $newPassword = $request->get('newPassword');
        $passwordAgain = $request->get('passwordAgain');
        if($newPassword !== $passwordAgain)
        {
            session()->flash('danger', '两次密码不一样');
            return back();
        }
        $leader = ClubLeader::where('id',$request->get('leader_id'))->first();
        if (Hash::check($password, $leader->password))
        {
            $leader->password = bcrypt($newPassword);
            $leader->save();

            session()->flash('success', '密码修改成功');
            return redirect()->route('resetpwd');
        }
        else {
            session()->flash('danger', '旧密码不正确');
            return redirect()->route('resetpwd');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $clubInfo = Club::findOrFail($id);
        return view('ClubLeader.editClubInfo', compact('clubInfo'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $clubInfo = Club::findOrFail($id);

        if ($request -> file('club_pic'))
        {
            $pic = $clubInfo -> club_pic;
            if ($pic != 'default.jpg')
            {
                Storage::disk('clubs_pic') -> exists($pic);
                Storage::disk('clubs_pic') -> delete($pic);
            }
            $this -> uploadAvatar($request);
            $clubInfo -> club_pic = $request -> get('club_name')
            . '.' . $request -> file('club_pic')
                -> getClientOriginalExtension();
            $clubInfo -> save();
        }

        $clubInfo -> update([
            'club_name' => $request -> get('club_name'),
            'club_intro' => $request -> get('club_intro'),
            'club_address' => $request -> get('club_address'),
            'club_phone' => $request -> get('club_phone'),
            'work_time' => $request -> get('work_time'),
        ]);
        $clubInfo -> save();

        session() -> flash('success', '更新成功');

        return redirect() -> route('clubLeader');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
    * Upload Pic to public_path
    * @param Request $request
    */
    public function uploadAvatar(Request $request)
    {
        if ($request->isMethod('post') OR $request->isMethod('patch')) {

            $pic = $request->file('club_pic');

            if ($pic->isValid()) {

                $realPath = $pic->getRealPath();
                $ext = $pic->getClientOriginalExtension();
                $clubName = $request->get('club_name');
                $fileName = $request->get('club_name'). '.' .$ext;
                Image::make($pic)->fit(300, 300)->save(public_path('/uploads/clubs_pic/clubs/') . $fileName);
            }
        }
    }
}
